/*
 *   Copyright (C) 2011 David Swords
 *	
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.net.*;
import java.util.Calendar;

public class LANdroidEEGEMGClient{
	
 	public static void main(String[] args) throws UnknownHostException, IOException {
 		String hri_message;
 		System.out.println("Connecting to localhost on 2000");
 		Socket emg_socket = new Socket("localhost", 2000);
 	//	if(emg_socket.isConnected()) {System.out.println("Connected to localhost on 2000");}
 	//	System.out.println("Connecting to 172.20.0.8 on 2000");
 		LANdroid no8 = new LANdroid(0, "172.20.0.8");
 	//	if(no8.isConnected()) {System.out.println("Connected to 172.20.0.8 on 2000");}
 	//	System.out.println("Connecting to 172.20.0.18 on 2000");
 		LANdroid no18 = new LANdroid(1, "172.20.0.18");
 	//	if(no18.isConnected()) {System.out.println("Connected to 172.20.0.18 on 2000");}
 	//	System.out.println("Connecting to 172.20.0.28 on 2000");
 		LANdroid no28 = new LANdroid(2, "172.20.0.28");
 	//	if(no28.isConnected()) {System.out.println("Connected to 172.20.0.28 on 2000");}
 	//	System.out.println("Connecting to 172.20.0.34 on 2000");
 		LANdroid no34 = new LANdroid(3, "172.20.0.34");
 	//	if(no34.isConnected()) {System.out.println("Connected to 172.20.0.34 on 2000");}
 	//	System.out.println("Connecting to 172.20.0.30 on 2000");
 		LANdroid no30 = new LANdroid(4, "172.20.0.30");
 	//	if(no30.isConnected()) {System.out.println("Connected to 172.20.0.30 on 2000");}
 		System.out.println("Connected...");
 		BufferedReader in_from_hri = new BufferedReader(new InputStreamReader(emg_socket.getInputStream()));
 	 		
 	 	while(true) {
 			hri_message = in_from_hri.readLine();
 			
	 		if(hri_message.contains("1L")) {
	 			// System.out.println("1L");
 	 			no8.left();
	 		}
	 			
 			if(hri_message.contains("1R")) {
 				// System.out.println("1R");
 				no8.right();
 			}

	 			
 			if(hri_message.contains("1B")) {
 				// System.out.println("1B");
 				no8.backward();
 			}

	 			
	 		if(hri_message.contains("1F")) {
	 			// System.out.println("1F");
 	 			no8.forward();
	 		}
	 		
	 		if(hri_message.contains("2L")) {
	 			// System.out.println("2L");
 	 			no18.left();
	 		}
	 			
 			if(hri_message.contains("2R")) {
 				// System.out.println("2R");
 				no18.right();
 			}

	 			
 			if(hri_message.contains("2B")) {
 				// System.out.println("2B");
 				no18.backward();
 			}

	 			
	 		if(hri_message.contains("2F")) {
	 			// System.out.println("2F");
 	 			no18.forward();
	 		}		
	 		
	 		if(hri_message.contains("3L")) {
	 			// System.out.println("3L");
 	 			no28.left();
	 		}
	 			
 			if(hri_message.contains("3R")) {
 				// System.out.println("3R");
 				no28.right();
 			}

	 			
 			if(hri_message.contains("3B")) {
 				// System.out.println("3B");
 				no28.backward();
 			}

	 			
	 		if(hri_message.contains("3F")) {
	 			// System.out.println("3F");
 	 			no28.forward();
	 		}
	 		
	 		if(hri_message.contains("4L")) {
	 			// System.out.println("4L");
 	 			no34.left();
	 		}
	 			
 			if(hri_message.contains("4R")) {
 				// System.out.println("4R");
 				no34.right();
 			}

	 			
 			if(hri_message.contains("4B")) {
 				// System.out.println("4B");
 				no34.backward();
 			}

	 			
	 		if(hri_message.contains("4F")) {
	 			// System.out.println("4F");
 	 			no34.forward();
	 		}
	 		
	 		if(hri_message.contains("5L")) {
	 			// System.out.println("5L");
 	 			no30.left();
	 		}
	 			
 			if(hri_message.contains("5R")) {
 				// System.out.println("5R");
 				no30.right();
 			}

	 			
 			if(hri_message.contains("5B")) {
 				// System.out.println("5B");
 				no30.backward();
 			}

	 			
	 		if(hri_message.contains("5F")) {
	 			// System.out.println("5F");
 	 	 		no30.forward();
	 		}
	 		
	 		if(hri_message.contains("6L")) {
	 			// System.out.println("6L");
 	 			no8.left();
 	 	 		no18.left();
 	 	 		no28.left();
 	 	 		no34.left();
 	 	 		no30.left();
	 		}
	 			
 			if(hri_message.contains("6R")) {
 				// System.out.println("6R");
 				no8.right();
 	 	 		no18.right();
 	 	 		no28.right();
 	 	 		no34.right();
 	 	 		no30.right();
 			}

	 			
 			if(hri_message.contains("6B")) {
 				// System.out.println("6B");
 				no8.backward();
 	 	 		no18.backward();
 	 	 		no28.backward();
 	 	 		no34.backward();
 	 	 		no30.backward();
 			}

	 			
	 		if(hri_message.contains("6F")) {
	 			// System.out.println("6F");
 	 			no8.forward();
 	 	 		no18.forward();
 	 	 		no28.forward();
 	 	 		no34.forward();
 	 	 		no30.forward();
	 		}
	 		
	 		if(hri_message.contains("S")) {
	 			//System.out.println("S");
 	 			no8.stop();
 	 	 		no18.stop();
 	 	 		no28.stop();
 	 	 		no34.stop();
 	 	 		no30.stop();
	 		}
 	 	}
	}
}